"ui";
const openReleasePage = require("release.js");
let webViewExpand = require("webViewExpand.js");

const pigInfo = storages.create("JD-PIG");
let autoRun = pigInfo.get("autoRun", true);
if (autoRun) {
    pigExecution = engines.execScriptFile('./api/task.js', {path: files.cwd()+'/api/'});
    toastLog('开启后台自运行');
}

var color = "#009688";
ui.layout(
    <drawer id="drawer">
        <vertical>
            <appbar>
                <tabs id="tabs"/>
            </appbar>
            <viewpager id="viewpager">
                <frame>
                    <vertical>
                        <frame w="*" h="*">
                            <webview id="webView" layout_below="title" />
                            <fab
                            id='gobackTreeBtn'
                            layout_width='wrap_content'
                            layout_height='wrap_content'
                            layout_gravity='bottom|left'
                            layout_margin='0dp'
                            backgroundTint='#3c78d8f0'
                            elevation='0dp'
                            src="@drawable/ic_keyboard_arrow_left_black_48dp"
                            color='#FAAB1A'
                            ></fab>
                            <fab
                            id='refreshTreeBtn'
                            layout_width='wrap_content'
                            layout_height='wrap_content'
                            layout_gravity='bottom|right'
                            layout_margin='0dp'
                            backgroundTint='#3c78d8f0'
                            elevation='0dp'
                            src="@drawable/ic_cached_black_48dp"
                            color='#FAAB1A'
                            ></fab>
                        </frame>
                    </vertical>
                </frame>
                <frame>
                    <vertical>
                        <frame w="*" h="*">
                            <webview id="pigWebView" layout_below="title" />
                            <fab
                            id='gobackPigBtn'
                            layout_width='wrap_content'
                            layout_height='wrap_content'
                            layout_gravity='bottom|left'
                            layout_margin='0dp'
                            backgroundTint='#3c78d8f0'
                            elevation='0dp'
                            src="@drawable/ic_keyboard_arrow_left_black_48dp"
                            color='#FAAB1A'
                            ></fab>
                            <fab
                            id='refreshPigBtn'
                            layout_width='wrap_content'
                            layout_height='wrap_content'
                            note="右下角"
                            layout_gravity='bottom|right'
                            note="margin"
                            layout_margin='0dp'
                            note="背景色"
                            backgroundTint='#3c78d8f0'
                            note="控制小白球的大小,值越大,白球越小"
                            elevation='0dp'
                            src="@drawable/ic_cached_black_48dp"
                            color='#FAAB1A'
                            ></fab>
                        </frame>
                    </vertical>
                </frame>
                <frame>
                    <vertical padding="16">
                        <horizontal gravity="center_vertical">
                            <View bg="#009688" h="*" w="10" />
                            <vertical padding="10 8" h="auto" w="0" layout_weight="1">
                                <text id="title" text="后台自动运行（尝鲜功能）" textColor="#222222" textSize="16sp" maxLines="1" />
                                <text text="目前支持：养猪、金果摇钱树" textColor="#999999" textSize="14sp" maxLines="1" />
                            </vertical>
                            <checkbox id="autoRunBtn" marginLeft="4" marginRight="6" checked="{{ autoRun }}" />
                        </horizontal>
                        <button text="日志" id="logger" style="Widget.AppCompat.Button.Colored" w="auto" />
                        <button text="退出" id="exit" w="auto" />
                    </vertical>
                </frame>
            </viewpager>
        </vertical>
    </drawer>
);

let pigExecution = null;
//设置滑动页面的标题
ui.viewpager.setTitles(["农场", "猪场", "控制中心"]);
//让滑动页面和标签栏联动
ui.tabs.setupWithViewPager(ui.viewpager);

/** 业务逻辑开始 */
ui.exit.on("click", () => {
    ui.webView.destroy();
    ui.pigWebView.destroy();
    if (pigExecution) {
        pigExecution.getEngine().forceStop();
    }
    ui.finish();
});
ui.autoRunBtn.on('check', (checked) => {
    if (checked) {
        pigExecution = engines.execScriptFile('./api/task.js', {path: files.cwd()+'/api/'});
        toastLog('开启后台自运行');
    } else {    
        if (pigExecution) {
            pigExecution.getEngine().forceStop();
        }
        toastLog('关闭后台自运行');
    }
    pigInfo.put("autoRun", checked);
});
ui.logger.on('click', () => {
    app.startActivity("console");
});

let treeUrl = "https://h5.m.jd.com/babelDiy/Zeus/3KSjXqQabiTuD1cJ28QskrpWoBKT/index.html?babelChannel=45&collectionId=519&lng=113.39183&lat=23.13042&sid=ae12c4c53cdb18ded15e85ad8547e2aw&un_area=19_1601_3633_0";
webViewExpand.init(ui.webView, ["inject/vconsole.ts", "inject/shareWx.ts", "inject/tree.ts"]);

let pigUrl = "https://u.jr.jd.com/uc-fe-wxgrowing/cloudpig/index/159384120202248288/?channelLV=qdy&shareId=vZoq-O6FkuUp62HuKheuTcAdoUJQ3Dik&sourceID=240&actflag=1AEE706F1D&isPay=N&utm_source=Android*url*1593841204607&utm_medium=jrappshare&utm_term=qqfriends";
webViewExpand.init(ui.pigWebView, ["inject/vconsole.ts", "inject/pig.ts"]);

ui.gobackTreeBtn.setOnClickListener(
    function(view){
        if (ui.webView.canGoBack()) { //点击返回按钮的时候判断有没有上一页
            ui.webView.goBack(); // goBack()表示返回webView的上一页面
        };
    }
);
ui.refreshTreeBtn.setOnClickListener(
    function(view){
        ui.webView.loadUrl(treeUrl);
    }
);
ui.gobackPigBtn.setOnClickListener(
    function(view){
        if (ui.pigWebView.canGoBack()) { //点击返回按钮的时候判断有没有上一页
            ui.pigWebView.goBack(); // goBack()表示返回webView的上一页面
        };
    }
);
ui.refreshPigBtn.setOnClickListener(
    function(view){
        ui.pigWebView.loadUrl(pigUrl);
    }
);

ui.viewpager.setOnPageChangeListener({
    onPageSelected: (position) => {
        console.log('进入页面：' + ["农场", "猪场", "控制中心"][position]);
    }
})